# !/usr/bin/python3
# -*-coding: utf-8-*-
# by QianFeng.newrain
#
'''
    Welcome QianFeng cloud computing
    use mail
    yag().send(to='收件人',
                subject='邮件主题',
                contents='邮件内容',
                attachments='附件')
'''

import yagmail
import hashlib
import os


def yag():
    maild = yagmail.SMTP(
        user='',
        password='',
        host='smtp.163.com',
        port='465',
        smtp_ssl=True)
    return maild.send


def md5sum(filelist, mode='r'):
    hash_list = []
    for hash in filelist:
        f = open(hash, mode='r')
        d = hashlib.md5()
        for buf in f.read():
            d.update(buf.encode())
        hash_list.append({hash: d.hexdigest()})
    if mode == 'r':
        return hash_list
    else:
        f = open('./hash', 'w')
        f.write(repr(hash_list))
        f.close()

if __name__ == '__main__':
    file = ['/Users/mingwang/Desktop/mail.py',
            '/Users/mingwang/Desktop/book.py']
    hash_sum = md5sum(file)

    if os.path.isfile('./hash'):
        f = open('./hash', 'r')
        old_list = eval(f.read())
        n = len(old_list) - 1
        warning = []
        while n > -1:
            if old_list[n] != hash_sum[n]:
                warning.append(old_list[n])
            n -= 1
        print(warning)
        yag()(to=[''],
              subject='重要数据篡改',
              contents=str(warning)
              )